package com.fxust.utils;

import com.fxust.common.CommonDefine;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

/**
 * @auto johnking
 * @create 2017-12-17 上午11:33
 **/
public class QuerySqlUtils {

    private static Logger logger = LoggerFactory.getLogger(QuerySqlUtils.class);

    public static void initSql(){
        // 读取静态库SQL语句
        String filepath = FileUtils.getSqlConfigXmlPath();
        readQueryDetailSQLXml(filepath);
        readQueryCountSQLXml(filepath);
    }


    private static void readQueryDetailSQLXml(String filepath) {
        File xmlFile = new File(filepath);
        SAXReader reader = new SAXReader();
        try {
            Document doc = reader.read(xmlFile);
            Element rootElement = doc.getRootElement();

            // 查询详情数据读取
            Element dataDetailElement = rootElement.element("DataDetail");
            String queryDetailSQL = dataDetailElement.getText();
            CommonDefine.queryDetailSql = queryDetailSQL.trim();

        } catch (Exception e) {
            logger.error("读取sql执行语句XML出错:", e);
        }
    }

    private static void readQueryCountSQLXml(String filepath) {
        File xmlFile = new File(filepath);
        SAXReader reader = new SAXReader();
        try {
            Document doc = reader.read(xmlFile);
            Element rootElement = doc.getRootElement();

            // 查询数据总量数据读取
            Element dataCountElement = rootElement.element("DataCount");
            String queryCountSQL = dataCountElement.getText();
            CommonDefine.queryCountSql = queryCountSQL.trim();
        } catch (Exception e) {
            logger.error("读取sql执行语句XML出错:", e);
        }
    }
}
